<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <?php
        require_once 'script2.php';
        ?>
    </head>
    <body>
        <?php
        require_once 'script.php';
        include("Hierarchy.php");
        include("File.php");
        
        if($_GET['actiune'] == "sterge"){
            $file = File::findById($_GET['id']);  // we get file info
            $parent = Hierarchy::findById($file->HierarchyId)->ParentId; // we get file hierachy
            File::deleteAll($_GET['id']); // we delete all files info, we can do this because differently from hierarchy they are not linked
            header('Location: index.php?hierarchy='.$parent);
        }
        
        if($_GET['hierarchy'] == null){
            $level = 1;
        } else {
            $level = $_GET['hierarchy'];
        }
        echo "<INPUT TYPE=BUTTON VALUE='Adugare tip Fisier' CLASS=b ONCLICK='window.location=\"addFileType.php\";'>";
        echo "<INPUT TYPE=BUTTON VALUE='Adaugare Fisier' CLASS=b ONCLICK='window.location=\"addFile.php?hierarchy=" . $level . "\";'>";
        
        $files = File::getCurrentFiles($level);    // depending on the level we get all files info
        ?>
        <table class="fancy3">
            <tr>
                <th>Nume</th>
                <th>Extensie</th>
                <th>Data</th>
                <th>Marime</th>
                <th>Optiuni</th>
            </tr>
            <?php
            $h = Hierarchy::findById($level); // depinding on the level we find all hierarchies
            if ($level!=1){
                echo "<tr>";
                    echo "<td>";
                        echo "<a href='index.php?hierarchy=".$h->ParentId."'>//</a>";
                    echo "</td>";
                    echo "<td>";
                    echo "</td>";
                    echo "<td>";
                    echo "</td>";
                    echo "<td>";
                    echo "</td>";
                    echo "<td>";
                    echo "</td>";
                echo "</tr>";
            }
            foreach($files as $file){
                echo "<tr>";
                    echo "<td>";
                        echo ($file->FileTypeId != 1) ? $file->Name : "<a href='index.php?hierarchy=".$file->HierarchyId."'>" . $file->Name . "</a>";
                    echo "</td>";
                    echo "<td>";
                        echo $file->getExtension(); // get file extension
                    echo "</td>";
                    echo "<td>";
                        echo $file->CreateDate;
                    echo "</td>";
                    echo "<td>";
                        echo ($file->Size != 0) ? $file->Size : "N/A";
                    echo "</td>";
                    echo "<td>";
                        echo "<a href = 'index.php?id=" . $file->Id . "&actiune=sterge'><img src='images/cross.png' alt='sterge'/></a>";
                        echo "<a href = 'copy.php?id=" . $file->Id . "&hierarchy=" . $h->Id . "'><img src='images/lupa.gif' alt='copiaza'/></a>";
                        echo "<a href = 'move.php?id=" . $file->Id . "&hierarchy=" . $h->Id . "'><img src='images/table_add.png' alt='copiaza'/></a>"; 
                    echo "</td>";                     
                echo "</tr>";
            }
            ?>
        </table>
    </body>
</html>
